// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //定义一个变量用来储存首页传递过来的值
    param1: '',
    list: '',
    // 获取当前时间
    currentTime: '',
    
    //判断搜索到的数据是否为空
    selectCartnot: true,
    Search_del: false,
    no_data: false,
    storeId:'',
    val:'',
  },
  
  //根据传递过来的值进行模糊查询获取到数据
  getData: function () {
    var userInfo = wx.getStorageSync('userInfo');
    var that = this;
    let param1 = this.data.param1;
    const storeId = this.data.storeId
    wx.request({
      url: `https://199shq.com/api/tproduct/store/search?name=${param1}&storeId=${storeId}`,  /// /tproduct/search?name=${param1}
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length == 0) {
          that.setData({
            no_data: true
          })
        }
        var list = res.data;
        if(param1){   
          wx.request({
            url: 'https://199shq.com/api/searchKeywords/addSearch',  //https://199shq.com
            method: 'POST',
            header:{
              'Content-Type': 'application/json',
              'token': wx.getStorageSync('token')
            },
            data:{
              "keyword" : param1,
              "createTime" : that.data.currentTime,
              "userId": userInfo.wxUserId,
              "userName":userInfo.nickName,
              "product" : list
            },
            success: function(res) {
              console.log(res.data);
            }
          })
        }
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
        if (list.length == 0) {
          that.setData({
            selectCartnot: false
          })
        }
      }
    })
    
    
  },

  //如果没有查询到数据则显示没有该数据信息
  selectCartnot: function () {
    if (this.data.list.length == 0) {
      this.setData({
        selectCartnot: false
      })
    }
  },
  
  //搜索事件定义
  Search: function (event) {
    const value = this.data.val;
    const storeId = this.data.storeId
    console.log('输入内容：', value);
    var userInfo = wx.getStorageSync('userInfo');
    var that = this;
    // let param1 = this.data.param1;
    wx.request({
      url: `https://199shq.com/api/tproduct/store/search?name=${value}&storeId=${storeId}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length == 0) {
          that.setData({
            no_data: true
          })
        }else{
          that.setData({
            no_data: false
          })
        }
        var list = res.data;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
        if (list.length == 0) {
          that.setData({
            selectCartnot: false
          })
        }
      }
    })
  },

  //点击触发搜索
  onInput: function () {
    const value = this.data.val;
    const storeId = this.data.storeId
    console.log('输入内容：', this.data);
    console.log('输入内容：', this.data.storeId);
    var userInfo = wx.getStorageSync('userInfo');
    var that = this;
    // let param1 = this.data.param1;
    wx.request({
      url: `https://199shq.com/api/tproduct/store/search?name=${value}&storeId=${storeId}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length == 0) {
          that.setData({
            no_data: true
          })
        }else{
          that.setData({
            no_data: false
          })
        }
        var list = res.data;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
        if (list.length == 0) {
          that.setData({
            selectCartnot: false
          })
        }
      }
    })
  },

  //点击获取input内容
  seldata: function (event) {
    const value = event.detail.value;
    this.setData({
      val: value,
      Search_del: true
    });
    if (value == '') {
      this.setData({
        Search_del: false
      });
      console.log(this.data.Search_del);
    }
  },

  //点击清空搜索框
  Search_del: function () {
    this.setData({
      val: '',
      Search_del: false
    });
  },

  //每次加载该页面都要清除购物车
  removeStorageSync: function () {
    var app = getApp();
    var user_id = String(app.globalData.userInfo.wxUserId);
    // 清除缓存数据
    wx.removeStorageSync(user_id);
  },
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     // 获取当前时间
     var now = new Date();
     // 格式化日期时间为 "yyyy-MM-dd HH:mm:ss"
    var year = now.getFullYear();
    var month = String(now.getMonth() + 1).padStart(2, '0');
    var day = String(now.getDate()).padStart(2, '0');
    var hours = String(now.getHours()).padStart(2, '0');
    var minutes = String(now.getMinutes()).padStart(2, '0');
    var seconds = String(now.getSeconds()).padStart(2, '0');
    
    var formattedTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

    // 更新页面数据中的 currentTime 字段
    this.setData({
      currentTime: formattedTime
    });
     

    // 获取传递的参数
    console.log(options);
    var param1 = options.param1;
    var storeId = options.storeId
    console.log('storeId:', storeId);
    this.setData({
      param1: param1,
      val:options.param1,
      storeId:storeId
    })
    this.getData();
    this.selectCartnot();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.removeStorageSync();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})